package cn.sundat.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;

import cn.sundat.model.Company;
import cn.sundat.model.PageBean;

public class CompanyDaoImpl extends BaseDaoImpl<Company> implements CompanyDao {

	/**
	 * 登录前的账号检测
	 */
	public int checkLogin(DetachedCriteria criteria) {
		//i为状态码,0表示未注册
		int i = 0;
		Company company = null;//查到的账号
		Integer comp_state=null;//账号状态码
		
		//查是否在数据库存在
		List<Company> list = (List<Company>) this.getHibernateTemplate().findByCriteria(criteria);
		
		//用公司名字登录,能登录到就不适用代码去检查登录
		if(list.size()>0) {
			company = list.get(0);
			comp_state = company.getComp_state();
			
			if(comp_state==1) {
				//注册并审核
				i=2;
			}else {
				//注册未审核
				i=1;
			}
		}
		
		return i;
	}

	/**
	 * 登录,登录之前有账号检测
	 */
	public List<Company> login(DetachedCriteria criteria) {
		return (List<Company>) this.getHibernateTemplate().findByCriteria(criteria);
	}

	public Company findByName(String comp_name) {
		List<Company> find = (List<Company>) this.getHibernateTemplate().find("from Company c where c.comp_name = ?", comp_name);
		if(find!=null && find.size()>0) {
			return find.get(0);
		}
		return null;
	}
	
	public List<Company> listCompany() {
		return (List<Company>) this.getHibernateTemplate().find("from Company");
	}

	@Override
	public List<Company> findCompanyByKey(String key,String value) {
		DetachedCriteria criteria=DetachedCriteria.forClass(Company.class);
		criteria.add(Restrictions.like(key,value,MatchMode.ANYWHERE));
		return (List<Company>) this.getHibernateTemplate().findByCriteria(criteria);
	}
	@Override
	public List<Company> findCompanyById(Long id) {
		return (List<Company>) this.getHibernateTemplate().find("from Company where comp_id=?", id);
	}

	@Override
	public List<Company> auditList() {
		DetachedCriteria criteria=DetachedCriteria.forClass(Company.class);
		criteria.add(Restrictions.in("comp_state", new Integer [] {0,3}));
		return (List<Company>) this.getHibernateTemplate().findByCriteria(criteria);
	}
	@Override
	public PageBean<Company> findByLevel(DetachedCriteria criteria, int currPage, Integer pageSize) {
		criteria.add(Restrictions.eq("comp_state", 1));
		return findByPage(currPage, pageSize, criteria);
	}
	@Override
	public void regApprove(Long id) {
		Company company= (Company) findCompanyById(id).get(0);
		if (company.getComp_state()==3) {
			Company delRecord = (Company) getHibernateTemplate().find("from Company where comp_state=1 and comp_code=?", company.getComp_code()).get(0);
			getHibernateTemplate().delete(delRecord);
			
			company.setComp_state(1);
			getHibernateTemplate().update(company);
		}
		
		if (company.getComp_state()==0) {
			company.setComp_state(1);
			getHibernateTemplate().update(company);
		}
	}
	@Override
	public PageBean<Company> findPbByName(Integer currPage, Integer pageSize, DetachedCriteria criteria) {
		return findByPage(currPage, pageSize, criteria);
	}
	@Override
	public void ApproveUpdateLevel(Long id,Integer nextLevel) {
		Company company= (Company) findCompanyById(id).get(0);
		company.setComp_level(nextLevel);
		company.getProfile().setPro_uplevel(null);
		getHibernateTemplate().update(company);
	}
	@Override
	public List<Company> updateLevelList() {
		DetachedCriteria criteria=DetachedCriteria.forClass(Company.class);
		criteria.createAlias("profile", "p");
		criteria.add(Restrictions.isNotNull("p.pro_uplevel"));
		return (List<Company>) getHibernateTemplate().findByCriteria(criteria);
	}
	
}
